define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'air/airport/index' + location.search,
                    add_url: 'air/airport/add',
                    edit_url: 'air/airport/edit',
                    // del_url: 'air/airport/del',
                    multi_url: 'air/airport/multi',
                    import_url: 'air/airport/import',
                    table: 'air_airport',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'en_name', title: __('En_name'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        // {field: 'airport_image', title: __('Airport_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'lng', title: __('Lng'), operate: false},
                        {field: 'lat', title: __('Lat'), operate: false},
                        {field: 'three_char', title: __('Three_char'), operate: 'LIKE'},
                        {field: 'four_char', title: __('Four_char'), operate: 'LIKE'},
                        {field: 'zone_name', title: __('Zone_name'), operate: false},
                        {field: 'info_type', title: __('Info_type'), operate: false},
                        {field: 'info_open', title: __('Info_open'), operate: false},
                        {field: 'info_altitude', title: __('Info_altitude'), operate: false},
                        {field: 'info_length', title: __('Info_length'), operate: false},
                        {field: 'info_opentime', title: __('Info_time'), operate: false},
                        {field: 'service_proper', title: __('Service_proper'), operate: false},
                        {field: 'service_open', title: __('Service_open'), operate: false},
                        {field: 'service_channel', title: __('Service_channel'), operate: false},
                        {field: 'service_stop', title: __('Service_stop'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, operate:false},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, operate:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                search:false,
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("change", "#c-area_id", function(data){
                console.log('data =>', $("#c-area_id").val())
                var areaId = $("#c-area_id").val()
                // 查找机场的3，4字码
                
                $.ajax({
                    url: "area/getAir",
                    type: "get",
                    data: {id: areaId},
                    success: function(data) {
                        if (data['three_code']) {
                            $("#c-three_char").val(data['three_code'])
                        }
                        if (data['four_code']) {
                            $("#c-four_char").val(data['four_code'])
                        }
                    }
                });
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-area_id", function(data){
                console.log('data =>', $("#c-area_id").val())
                var areaId = $("#c-area_id").val()
                // 查找机场的3，4字码
                
                $.ajax({
                    url: "area/getAir",
                    type: "get",
                    data: {id: areaId},
                    success: function(data) {
                        if (data['three_code']) {
                            $("#c-three_char").val(data['three_code'])
                        }
                        if (data['four_code']) {
                            $("#c-four_char").val(data['four_code'])
                        }
                    }
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
